package org.jeecg.modules.youhjfaf.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.order.entity.KuybOrderPolicy;
import org.jeecg.modules.order.service.IKuybOrderPolicyService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.youhj.entity.KuybYouhj;
import org.jeecg.modules.youhj.service.IKuybYouhjService;
import org.jeecg.modules.youhjfaf.entity.KuybYouhjFaf;
import org.jeecg.modules.youhjfaf.service.IKuybYouhjFafService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 优惠劵发放
 * @Author: jeecg-boot
 * @Date: 2022-01-16
 * @Version: V1.0
 */
@Api(tags = "优惠劵发放")
@RestController
@RequestMapping("/youhjfaf/kuybYouhjFaf")
@Slf4j
public class KuybYouhjFafController extends JeecgController<KuybYouhjFaf, IKuybYouhjFafService> {
    @Autowired
    private IKuybYouhjFafService kuybYouhjFafService;

    @Autowired
	IKuybYouhjService kuybYouhjService;

    @Autowired
    IKuybOrderPolicyService kuybOrderPolicyService;

    @GetMapping(value = "/getClientInfo")
    public Result<?> getClientInfo(String kehsjh) {
        KuybOrderPolicy kuybOrderPolicy = kuybOrderPolicyService.getOne(new LambdaQueryWrapper<KuybOrderPolicy>()
                .eq(KuybOrderPolicy::getBeibxrlxdh, kehsjh)
                .orderByDesc(KuybOrderPolicy::getCreateTime), false);

        if(kuybOrderPolicy != null) {
            return Result.OK(kuybOrderPolicy);
        }

        return Result.error("没查到数据");
    }

    // 我的优惠劵列表
    @GetMapping(value = "/wodyhjlb")
    public Result<?> wodyhjlb(String kehsjh) {
        if(kehsjh != null && !kehsjh.equals("")) {
            ;

        } else {
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (sysUser != null) {
                kehsjh = sysUser.getUsername();
            }
        }

        List<KuybYouhjFaf> list = kuybYouhjFafService.list(new LambdaQueryWrapper<KuybYouhjFaf>()
                .eq(KuybYouhjFaf::getYongh, kehsjh)
                .gt(KuybYouhjFaf::getFafsl, 0));

        if (list != null) {
            list.forEach(item -> {
                KuybYouhj kuybYouhj = kuybYouhjService.getById(item.getYouhj());
                if (kuybYouhj != null) {
                    item.setYouhjmingc(kuybYouhj.getMingc());
                    item.setYouhjtup(kuybYouhj.getTup());
                    item.setYouhjshiysm(kuybYouhj.getShiysm());
                }
            });

            return Result.OK(list);
        }

        return Result.error("没查到优惠劵");
    }

    /**
     * 分页列表查询
     *
     * @param kuybYouhjFaf
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "优惠劵发放-分页列表查询")
    @ApiOperation(value = "优惠劵发放-分页列表查询", notes = "优惠劵发放-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(KuybYouhjFaf kuybYouhjFaf,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<KuybYouhjFaf> queryWrapper = QueryGenerator.initQueryWrapper(kuybYouhjFaf, req.getParameterMap());
        Page<KuybYouhjFaf> page = new Page<KuybYouhjFaf>(pageNo, pageSize);
        IPage<KuybYouhjFaf> pageList = kuybYouhjFafService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @Autowired
    ISysUserService sysUserService;

    /**
     * 添加
     *
     * @param kuybYouhjFaf
     * @return
     */
    @AutoLog(value = "优惠劵发放-添加")
    @ApiOperation(value = "优惠劵发放-添加", notes = "优惠劵发放-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody KuybYouhjFaf kuybYouhjFaf) {
        SysUser sysUser = sysUserService.getUserByName(kuybYouhjFaf.getYongh());
        if(sysUser != null)
            kuybYouhjFaf.setYonghsjh(sysUser.getPhone());

        kuybYouhjFafService.save(kuybYouhjFaf);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param kuybYouhjFaf
     * @return
     */
    @AutoLog(value = "优惠劵发放-编辑")
    @ApiOperation(value = "优惠劵发放-编辑", notes = "优惠劵发放-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody KuybYouhjFaf kuybYouhjFaf) {
        kuybYouhjFafService.updateById(kuybYouhjFaf);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "优惠劵发放-通过id删除")
    @ApiOperation(value = "优惠劵发放-通过id删除", notes = "优惠劵发放-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        kuybYouhjFafService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "优惠劵发放-批量删除")
    @ApiOperation(value = "优惠劵发放-批量删除", notes = "优惠劵发放-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.kuybYouhjFafService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "优惠劵发放-通过id查询")
    @ApiOperation(value = "优惠劵发放-通过id查询", notes = "优惠劵发放-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        KuybYouhjFaf kuybYouhjFaf = kuybYouhjFafService.getById(id);
        if (kuybYouhjFaf == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(kuybYouhjFaf);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param kuybYouhjFaf
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, KuybYouhjFaf kuybYouhjFaf) {
        return super.exportXls(request, kuybYouhjFaf, KuybYouhjFaf.class, "优惠劵发放");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, KuybYouhjFaf.class);
    }

}
